package com.example.lenovo.mywechat.homepage;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.view.View;

import com.example.lenovo.mywechat.R;

public class MouldPage {
    private FragmentManager fragmentManager;

    private Fragment contentFragment;//主页内容框架。
    private MouldBottomBtn mouldBottomBtn;//主页下方按钮模板。

    //以下为状态数据：1.选中时数据。2.未被选中时数据。
    private int normalImageResource;//未选中时ImageButton的图片资源
    private int pressedImageResource;//选中时ImageButton的图片资源
    private final static int normalTextColor = Color.parseColor("#000000");//未选中时TextView文字颜色
    private final static int pressedTextColor = Color.parseColor("#009900");//选中时TextView文字颜色

    public MouldPage(Activity mActivity, FragmentManager fragmentManager, Fragment contentFragment, int llRID, int ibRID, int tvRID) {
        this.fragmentManager = fragmentManager;
        this.mouldBottomBtn = new MouldBottomBtn(mActivity, llRID, ibRID, tvRID);
        this.contentFragment = contentFragment;

        //向FragmentManager增加要管理的页面，并设置为隐藏
        FragmentTransaction fragmentTransaction = this.fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.content_fragment, this.contentFragment);
        fragmentTransaction.hide(this.contentFragment);
        fragmentTransaction.commit();
    }

    public void setNormalImageResource(int normalImageResource) {
        this.normalImageResource = normalImageResource;
    }

    public void setPressedImageResource(int pressedImageResource) {
        this.pressedImageResource = pressedImageResource;
    }

    //当选中的是此页面时调用的函数
    public void showPage() {
        this.mouldBottomBtn.changeImageResource(pressedImageResource);
        this.mouldBottomBtn.changeTextColor(this.pressedTextColor);

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.show(contentFragment);
        fragmentTransaction.commit();
    }

    //当选中的不是此页面时调用的函数
    public void hidePage() {
        this.mouldBottomBtn.changeImageResource(normalImageResource);
        this.mouldBottomBtn.changeTextColor(this.normalTextColor);

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.hide(contentFragment);
        fragmentTransaction.commit();
    }

    //为主页下方一个按钮增加监听事件
    public void addOnClickListener (View.OnClickListener onClickListener) {
        mouldBottomBtn.addOnClickListener(onClickListener);
    }
}
